
#ifndef _TTG_DEVICES_GPU_CUDA_CUDATHREAD_
#define _TTG_DEVICES_GPU_CUDA_CUDATHREAD_
#include "Common/Definitions.h"

#include "Common/Structures.h"
#include "Runtime.Low/Threads.h"

namespace ttg_internal
{
	namespace devices
	{
		//Thread with active CUDA-context.
		class CudaThread :public ttg::threads::Thread
		{
			private:
				ttg::threads::SlowCriticalSection locker;
				ttg::threads::SlowEvent ev1;	//waiting
				ttg::threads::SlowEvent ev2;	//rising
				ttg::structures::InvokableObject *action;
				bool is_free;
				ttg::threads::FastCriticalSection is_free_locker;
			protected:
				//Starts waiting.
				virtual void work();
			public:
				//Creates and starts CUDA thread.
				inline CudaThread()
					:ttg::threads::Thread(true)
				{ is_free = true; action = NULL; }
				//Tries to skip "is_free" flag. In case of success returns "true".
				//Note: it's internal method and should be used by CudaManager only!
				bool tryAcquire();
				//Performs requred action in CUDA-contexted thread (on corresponded CUDA device).
				//Note: "action" will be deleted.
				void perform(ttg::structures::InvokableObject *action);
		};
	}
}

#endif
